﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using database.Entities;
using System.Data.OracleClient;
using System.Data;
using database;

namespace database.DAOImpl
{
    class UserDAOImpl
    {
        public UserDAOImpl()
        {

        }

        public void insert(Users u)
        {
            DBUtils util = new DBUtils();
            util.ConnOpen();
            string sql = "insert into USERS (USERNAME, PASSWORD, AUTHORITY, REALNAME, IDENTITYNUMBER) values (:username, :password, :authority, :realname, :identitynumber)";
            OracleCommand cmd = new OracleCommand(sql, util.getConn());
            OracleParameter parUser = new OracleParameter(":username", u.UserName);
            OracleParameter parPassword = new OracleParameter(":password",u.Password);
            OracleParameter parAuthority = new OracleParameter(":authority",u.Authority);
            OracleParameter parRealname = new OracleParameter(":realname", u.RealName);
            OracleParameter parIdentitynumber = new OracleParameter(":identitynumber", u.IdentityNumber);
            cmd.Parameters.Add(parUser);
            cmd.Parameters.Add(parPassword);
            cmd.Parameters.Add(parAuthority);
            cmd.Parameters.Add(parRealname);
            cmd.Parameters.Add(parIdentitynumber);
            cmd.CommandText = sql;
            cmd.ExecuteNonQuery();
            util.ConnClose();
        }

         public Boolean login(string username, string password)
        {
            DBUtils util = new DBUtils();
            util.ConnOpen();
            string sql = "select *from USERS where USERNAME = :username and PASSWORD = :password";
            OracleCommand cmd = new OracleCommand(sql, util.getConn());
            OracleParameter parUser = new OracleParameter(":username", username);
            OracleParameter parPassword = new OracleParameter(":password", password);
            cmd.Parameters.Add(parUser);
            cmd.Parameters.Add(parPassword);
            cmd.CommandText = sql;

            OracleDataReader odr = cmd.ExecuteReader();
            if (odr.Read())
            {
                //Global.u.ID = (odr.GetInt32(0));
                //Global.u.UserName = (odr.GetString(1).ToString());
                //Global.u.Password = (odr.GetString(2));
                //Global.u.Authority = (odr.GetInt32(3));
                //Global.u.RealName = (odr.GetString(4));
                //Global.u.IdentityNumber = (odr.GetString(5));
                return true;
            }

            util.ConnClose();

            return false;
        }
    }
}
